﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;

namespace MyWeb.Common
{
    public class UploadsDownloads
    {
        /// <summary>
        /// Dùng cho viec upload file từ client đến sever như file text, dzữ liệu đơn giản
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="uploadUrl"></param>
        static public void UploadFile(string localFile, string uploadUrl)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);
            req.Method = "PUT";
            req.AllowWriteStreamBuffering = true;

            // Retrieve request stream and wrap in StreamWriter
            Stream reqStream = req.GetRequestStream();
            StreamWriter wrtr = new StreamWriter(reqStream);

            // Open the local file
            StreamReader rdr = new StreamReader(localFile);

            // loop through the local file reading each line
            //  and writing to the request stream buffer
            string inLine = rdr.ReadLine();
            while (inLine != null)
            {
                wrtr.WriteLine(inLine);
                inLine = rdr.ReadLine();
            }

            rdr.Close();
            wrtr.Close();

            req.GetResponse();
        }
        /// <summary>
        /// Upload File từ client theo dạng binary lên sever như file hình ảnh v.v....
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="uploadUrl"></param>
        static public void UploadFileBinary(string localFile, string uploadUrl)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(uploadUrl);

            req.Method = "PUT";
            req.AllowWriteStreamBuffering = true;

            // Retrieve request stream
            Stream reqStream = req.GetRequestStream();

            // Open the local file
            FileStream rdr = new FileStream(localFile, FileMode.Open);

            // Allocate byte buffer to hold file contents
            byte[] inData = new byte[4096];

            // loop through the local file reading each data block
            //  and writing to the request stream buffer
            int bytesRead = rdr.Read(inData, 0, inData.Length);
            while (bytesRead > 0)
            {
                reqStream.Write(inData, 0, bytesRead);
                bytesRead = rdr.Read(inData, 0, inData.Length);
            }

            rdr.Close();
            reqStream.Close();

            req.GetResponse();
        }
        /// <summary>
        /// download file từ server đến client theo dạng bình thường
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="downloadUrl"></param>
        static public void DownloadFile(string localFile, string downloadUrl)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
            req.Method = "GET";

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            // Retrieve response stream and wrap in StreamReader
            Stream respStream = resp.GetResponseStream();
            StreamReader rdr = new StreamReader(respStream);

            // Create the local file
            StreamWriter wrtr = new StreamWriter(localFile);

            // loop through response stream reading each line
            //  and writing to the local file
            string inLine = rdr.ReadLine();
            while (inLine != null)
            {
                wrtr.WriteLine(inLine);
                inLine = rdr.ReadLine();
            }

            rdr.Close();
            wrtr.Close();
        }
        /// <summary>
        /// download file từ server đến client theo dạng file binary
        /// </summary>
        /// <param name="localFile"></param>
        /// <param name="downloadUrl"></param>
        static public void DownloadFileBinary(string localFile, string downloadUrl)
        {
            HttpWebRequest req = (HttpWebRequest)WebRequest.Create(downloadUrl);
            req.Method = "GET";

            HttpWebResponse resp = (HttpWebResponse)req.GetResponse();

            // Retrieve response stream
            Stream respStream = resp.GetResponseStream();

            // Create local file
            FileStream wrtr = new FileStream(localFile, FileMode.Create);

            // Allocate byte buffer to hold stream contents
            byte[] inData = new byte[4096];

            // loop through response stream reading each data block
            //  and writing to the local file
            int bytesRead = respStream.Read(inData, 0, inData.Length);
            while (bytesRead > 0)
            {
                wrtr.Write(inData, 0, bytesRead);
                bytesRead = respStream.Read(inData, 0, inData.Length);
            }

            respStream.Close();
            wrtr.Close();
        }
    }
}
